package net.xdclass.websocket.service;

import net.xdclass.websocket.controller.v5.StockService;
import net.xdclass.websocket.controller.v6.User;
import net.xdclass.websocket.model.InMessage;
import net.xdclass.websocket.model.OutMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @description: 简单消息模板，用来推送消息
 * @author: Yz
 * @create: 2020-06-30 14:49
 **/
@Service
public class WebSocketService {

    @Autowired
    private SimpMessagingTemplate template;

    public void sendTopicMessage(String dest,InMessage message){

        for (int i = 0; i < 20 ; i++) {
            template.convertAndSend(dest,new OutMessage(message.getContent() + i));
        }

    }

    public void sendChatMessage(InMessage message){
        template.convertAndSend("/chat/single/"+message.getTo(),new OutMessage(message.getFrom()+" 发送："+message.getContent()));
    }


    /**
     * 获取系统信息，推送给客户端
     * 每隔3秒执行
     */
    @Scheduled(fixedRate = 3000)
    public void sendServerInfo(){
        //可用核数
        int processors = Runtime.getRuntime().availableProcessors();
        //可用的jvm内存数
        long freeMemory = Runtime.getRuntime().freeMemory();
        //最大的内存
        long maxMemory = Runtime.getRuntime().maxMemory();

        String message = String.format("服务器可用处理器：%s；虚拟机空闲内存大小：%s；已用内存大小：%s",processors,freeMemory,maxMemory);

        template.convertAndSend("/topic/server_info",new OutMessage(message));
    }

    public void sendStockInfo() {

        Map<String, String> stockInfoMap = StockService.getStockInfo();
        String msgTpl = "名称: %s ; 价格: %s元 ; 最高价: %s ; 最低价: %s ; 涨跌幅: %s ; 市盈率TTM: %s ; 总市值: %s";

        if (null != stockInfoMap) {
            String msg = String.format(msgTpl, stockInfoMap.get("prod_name"), stockInfoMap.get("last_px"), stockInfoMap.get("high_px"),
                    stockInfoMap.get("low_px"), stockInfoMap.get("px_change"), stockInfoMap.get("market_value"), stockInfoMap.get("amplitude") );

            template.convertAndSend("/topic/stock_info",new OutMessage(msg));
        }
    }

    /**
     * 发送在线用户
     * @param onlineUser
     */
    public void sendOnlineUser(Map<String, User> onlineUser){
        String msg = "";
        for (Map.Entry<String, User> entry : onlineUser.entrySet()) {
            User user = entry.getValue();
            msg = msg.concat( user.getUsername()+" || ");
        }
        template.convertAndSend("/topic/onlineuser",new OutMessage(msg));
    }

    /**
     * 用于多人聊天
     * @param message
     */
    public void sendTopicChat(InMessage message){
        String msg = message.getFrom() + " 发送：" + message.getContent();
        template.convertAndSend("/topic/chat",new OutMessage(msg));
    }
}
